package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Food;
import com.itheima.pojo.Sport;
import com.itheima.service.FoodBaseService;
import com.itheima.utils.POIUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/foodBase")
public class FoodBaseController {

    @Reference
    private FoodBaseService foodBaseService;

    //上传excel文件
    @PostMapping("/upload")
    public Result upload(@RequestParam("excelFile") MultipartFile excelFile){
        try {
            //获取运动库中的所有信息
            List<String[]> excel = POIUtils.readExcel(excelFile);
            List<Food> foodList = new ArrayList<>();
            if(excel!=null&&excel.size()>0){
                //获取每行的数据,封装到一个运动对象中去
                for (String[] strings : excel) {
                    Food food = new Food();
                    food.setId(null);
                    food.setName(strings[0]);
                    food.setRemark(strings[1]);
                    food.setAmount(strings[2]);
                    food.setAge(strings[3]);
                    foodList.add(food);
                }
                //如果sportList不为空,则上传数据
                if(foodList != null && foodList.size()>0)
                    foodBaseService.addFoodList(foodList);
                return new Result(true, MessageConstant.UPLOAD_SUCCESS);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.UPLOAD_FAIL);
        }
        return null;
    }

    @GetMapping("/findPage")
    public Result findPage(QueryPageBean queryPageBean){
        try{
            PageInfo page = foodBaseService.findPage(queryPageBean);
            return new Result(true,"查询膳食库成功",page);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,"查询膳食库失败");
        }
    }

    //添加单个膳食
    @PostMapping("/addOneFood")
    public Result addOneFood(@RequestBody Food food){
        try{
            foodBaseService.addOneFood(food);
            return new Result(true , "新增膳食成功");
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,"新增膳食失败");
        }
    }

    //根据id查询
    @GetMapping("/findById/{id}")
    public Result findById(@PathVariable Integer id){
        try{
            Food food = foodBaseService.findById(id);
            return new Result(true,MessageConstant.QUERY_FOOD_SUCCESS,food);
            }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_FOOD_FAILED);
        }
    }

    //修改food
    @PutMapping("/update")
    public Result update(@RequestBody Food food){
        try{
            foodBaseService.update(food);
            return new Result(true,MessageConstant.UPDATE_FOOD_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.UPDATE_FOOD_FAILED);
        }
    }

    //根据id删除
    @DeleteMapping("/deleteById/{id}")
    public Result deleteById(@PathVariable Integer id){
        try{
            foodBaseService.deleteById(id);
            return new Result(true,MessageConstant.DELETE_FOOD_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.DELETE_FOOD_FAILED);
        }
    }
}
